<?php
namespace App\Game;

class Worker{
    public $redis;

    public function __construct($redis)
    {
        $this->redis =$redis;
    }
    public function connect($server,$fd,$swoole_client)
    {
        $server->task(json_encode(['fd'=>$fd,'action'=>'connect']));

    }
    public function receive($server,$fd,$from_id,$data)
    {
        //判断是否是第一次连接
        //查询redis是否有这个人存在
        $data_obj = json_decode($data);
        $data_obj->fd = $fd;
        print_r($data_obj);
        if($this->redis->sIsMember('member',$fd)){
            //业务投递task
            $server->task(json_encode($data_obj));
        }else{
            //判断参数是否合法
            if(is_object($data_obj) && $data_obj->action == 'login' && $data_obj->mobile > 0 && $data_obj->token != ''){
                //登陆 投递task
                $data_obj->action = 'login';
                $server->task(json_encode($data_obj));
            }else{
                $data_obj->action = 'auth_fail';
                $server->task(json_encode($data_obj));
            }
        }
    }

    public function close($server,$fd)
    {
        $server->task(json_encode(['fd'=>$fd,'action'=>'logout']));

    }
}